/* *
 * 常世超(作者)
 * 2018-05-22(开始日期)
 * 2018-05-22(最终更改日期)
 * 常世超(最后更改者)
 * */
	if(Auth()){
		var id = user_info.id;
		var phone = user_info.phone;
		var uuid = user_info.uuid
	} else{
		toClientLogin(location.href)
	}

	$('.head-img').on('click',function(){
		window.history.go(-1);
	})

	// 选择收货人
	$('.add').click(function(){
		window.location.href=rootdomain+'html/product-order/management.html'
	})

	$('.add-2').click(function(){
		window.location.href=rootdomain+'html/product-order/management.html'
	})
//渲染订单商品列表
	var spId;

	// 订单信息
	var sphj;
	var mrshr;
	var shopCartIds = ''
	var topicid = globalFun.GetQueryString('topicid')//从课题详情跳转商品购买下单分润所需
	var hunterid = globalFun.GetQueryString('share')//从课题详情跳转商品购买下单分润所需
	console.log(hunterid)
	var tid = !topicid?"":topicid;
	var hid = !hunterid?"":hunterid;
	console.log(hid)
	console.log(tid)

	var dataObj = {
		id:id,
		shopCartIds:shopCartIds,
		uuid:uuid,
		phone:phone,
		type:1
	}

	var shopCartIds;
	shopCartIds = JSON.parse(sessionStorage.getItem("shopCartIds"));


	// 订单信息
	var lists = {list:''};
	var item = []

	var item2 = {
				"hunterId":hid,
				"merchantId":shopCartIds.merchantId,
				"topicId":tid,
				"message":"不想收货",
				"psList":[{"count":shopCartIds.buyNum,"payType":0,"psId":spId,"topcart":0}]
			}
	var zhuangt = sessionStorage.getItem('zhuangt');



	if(zhuangt == 1){
		shopCartIds = JSON.parse(sessionStorage.getItem("shopCartIds"))
		spId = shopCartIds.spId
		console.log(shopCartIds)

		var shopCartIds = shopCartIds;

		var content='<div class="spxx1"><p class="spxx1-wen">商品信息</p></div>';

		var maney = shopCartIds.orderTotalPrice
		// console.log(maney)

		localStorage.setItem("orderTotalPrice",maney)


		$('.jine-a').html(shopCartIds.orderTotalPrice);
		$('.heji-a').html(shopCartIds.productTotalPrice);
		$('.yunfei-a').html(shopCartIds.freightTotalPrice);

		var mrshr = shopCartIds.addressFlag;
		console.log(mrshr)
		if(mrshr == 0){
			$('.add').show();
			$('.add-2').hide();
			} else if(mrshr == 1){
				$('.add-2').show();
				$('.add').hide();
				$('.shr').html("收货人:"+shopCartIds.userName);
				$('.sjh').html(shopCartIds.mobile);
//				$('.moren').hide()
				console.log(shopCartIds.address)
				if(shopCartIds.defaultAddress){
					$('.shdz').html("收货地址："+shopCartIds.defaultAddress);
				}else{
					$('.shdz').html("收货地址："+shopCartIds.address);
				}

			}


			var dpid = [];
				dpid.push(shopCartIds.merchantId);
				item2 = {
					"hunterId":hid,
					"merchantId":shopCartIds.merchantId,
					"topicId":tid,
					"message":"",
					"orderSource":1,
					"psList":[{"count":shopCartIds.num,"payType":1,"productType":0,"psId":spId,"topcart":0}]
				}
				if(topicid==null){
					item2.topicId = '';
				}else{
					item2.topicId = topicid;
				}
				// 安卓
				if(mark == 0){
					item2.orderSource = 2;
				}
				// 网页
				if(mark == 2){
					item2.orderSource = 3;
				}
				item=item2

			if(null==shopCartIds.img || undefined==shopCartIds.img || ''==shopCartIds.img){
				shopCartIds.img='../../img/d_cart4.png';
			}
			content+='<div class="spxx2"><div class="spxx2-a"><div class="spxx-img"><img src="'+shopCartIds.img+'"></div><div class="spxx-right"><div class="spxx-wen"><p class="wen1">'+shopCartIds.pName+'</p><p class="maney">¥<span class="maney-a">'+Number(shopCartIds.salePrice).toFixed(2)+'</span></p></div><div class="spxx-foot"><p class="color">'+shopCartIds.psName+'</p><p class="num">'+shopCartIds.num+'</p></div></div></div></div>'
			if($('.num').html(undefined)){
				$('.num').html()
			}
		console.log(item);
		// item = JSON.stringify(item);
		lists.list = item;
		lists = JSON.stringify(lists);

		$('.spxx').html(content);

	}else if(zhuangt==2){
		shopCartIds = JSON.parse(sessionStorage.getItem("shopCartIds"))
		console.log(shopCartIds)
		var cart_list=shopCartIds.shopCartList;
		var len=cart_list.length;
		console.log(len)
		for(var i = 0; i < cart_list.length; i++){
			spId = cart_list[i].spId;
		}
		console.log(spId)

		var content='<div class="spxx1"><p class="spxx1-wen">商品信息</p></div>';

		var maney = shopCartIds.orderTotalPrice
		console.log(maney)

		localStorage.setItem("orderTotalPrice",maney)

		$('.jine-a').html(shopCartIds.orderTotalPrice);
		$('.heji-a').html(shopCartIds.productTotalPrice);
		$('.yunfei-a').html(shopCartIds.freightTotalPrice);

		var mrshr = shopCartIds.addressFlag;
		console.log(mrshr)
		if(mrshr == 0){
			$('.add').show();
			$('.add-2').hide();
			} else if(mrshr == 1){
				$('.add-2').show();
				$('.add').hide();
				$('.shr').html("收货人:"+shopCartIds.userName);
				$('.sjh').html(shopCartIds.mobile);
				if(shopCartIds.defaultAddress){
					$('.shdz').html("收货地址："+shopCartIds.defaultAddress);
				}else{
					$('.shdz').html("收货地址："+shopCartIds.address);
				}
			}


		var dpid = [];



		for(var i = 0; i < len ; i++){
			item2 = {};

			if(dpid.indexOf(cart_list[i].merchantId) == -1){
				dpid.push(cart_list[i].merchantId);
				item2 = {
					"hunterId":cart_list[i].hunterId,
					"merchantId":cart_list[i].merchantId,
					"topicId":"",
					"message":"",
					"orderSource":1,
					"psList":[{"count":cart_list[i].buyNum,"payType":1,"productType":0,"psId":cart_list[i].spId,"topcart":0}]
				}
				// 安卓
				if(mark == 0){
					item2.orderSource = 2;
				}
				// 网页
				if(mark == 2){
					item2.orderSource = 3;
				}
				item.push(item2)
			} else{
				for(var j in item){
					if(item[j].merchantId == cart_list[i].merchantId){
						item[j].psList.push({"count":cart_list[i].buyNum,"payType":1,"productType":0,"psId":cart_list[i].spId,"topcart":0})
					}
				}
			}

			if(null==cart_list[i].img || undefined==cart_list[i].img || ''==cart_list[i].img){
				cart_list[i].img='../../img/d_cart4.png';
			}
			content+='<div class="spxx2"><div class="spxx2-a"><div class="spxx-img"><img src="'+cart_list[i].img+'"></div><div class="spxx-right"><div class="spxx-wen"><p class="wen1">'+cart_list[i].pName+'</p><p class="maney">¥<span class="maney-a">'+Number(cart_list[i].salePrice).toFixed(2)+'</span></p></div><div class="spxx-foot"><p class="color">'+cart_list[i].psName+'</p><p class="num">'+cart_list[i].buyNum+'</p></div></div></div></div>'
		}
		console.log(item);
		// item = JSON.stringify(item);
		lists.list = item;
		lists = JSON.stringify(lists);
		$('.spxx').html(content);
	}

	// }

	var orderIds;
	var oId;

	$(document).on('click','.btn',function(){
		if($('.btn').hasClass('abc')){
			$('.btn').removeClass('abc');
			setTimeout(function () {
                   			 $('.btn').addClass('abc');
                 		}, 2000);
			if(shopCartIds.addressId == '' || shopCartIds.addressId == null || shopCartIds.addressId == undefined){
		         		tips("请选择收货地址");
				$('.btn').addClass('abc');
	 			return false;
	        		} else{

		        		var dataObj = {
		        		id:user_info.id,
						uuid:user_info.uuid,
						phone:user_info.phone,
						aId:shopCartIds.addressId,
						list:lists,
						type:0
					}

					console.log(dataObj)

					$.ajax({
						type:"post",
						url: apicom + "mobile/after/order/createOrder1",
//						url:"http://192.168.30.199:10001/mobile/after/order/createOrder1",
						data:dataObj,
						async: false,
						dataType: "json",
						success:function(res){
							console.log(res);
							if(typeof(res) == 'string'){
								res = JSON.parse(res);
							}

							if(res.code == 1){
								localStorage.setItem("createdTime",res.content.createdTime);
								orderIds = res.content.orderIds;//订单号
								oId =  res.content.id;//订单ID
//								console.log(orderIds)
								// localStorage.setItem("orderIda",orderIda)
								window.location.href= rootdomain + 'html/product-order/payment.html?orderIds='+orderIds+'&oid='+oId;
								// $('.btn').addClass('abc')

							} else{
								tips(res.msg);
							}


						},
						error:function(err){
							console.log(err);
						}
					})
	        			}
			}

		  });

